<?php
/*
	插件概览
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['type']=array('1'=>'物联58打印机','2'=>'物联58打印机NEW','3'=>'物联80打印机');		
if($this->plugin_exists('jy_feieyun')){
	$return['type'][7]='飞蛾80打印机';
	$return['type'][9]='飞蛾58打印机';
	$return['printer7']=cfc('jy_feieyun')->getlist(0,0,1);
}
if($this->plugin_exists('jy_xpyun')){
	$return['type'][8]='芯烨58打印机';
	$return['type'][10]='芯烨80打印机';
	$return['printer8']=model('jy_xpyun/model')->getlist(0,0,1);
}
$return['print_type']=array('1'=>'下单后打印','2'=>'付款后打印','3'=>'确认收货后打印','4'=>'收银结账单','5'=>'收银付款小票','7'=>'退商品小票','18'=>'手动打印');
$return['status']=array('0'=>'关闭','1'=>'开启');
$return['duo']=array('1'=>'打印','0'=>'不打印');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$this->merchid);
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_goodserpfw_printer',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$htmls=array(
			'type'=>'list',
			'tabs'=>array(
				array('op'=>'list','title'=>'打印任务管理'),
				array('op'=>'edit','title'=>'添加打印任务'),
			),
			'list'=>array('data'=>'list','list'=>array(
				array('name'=>'title','title'=>'任务名称','type'=>'string'),
				array('name'=>'type','title'=>'关联打印机类型','type'=>'radio','options'=>'type'),
				array('name'=>'print_nums','title'=>'打印联数','type'=>'string'),
				array('name'=>'print_status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_goodserpfw_printer',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['goods']=json_decode($item['goods'],true);
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'打印机设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'打印任务管理'),
			array('op'=>'edit','title'=>'添加打印任务')
		),
		'edit'=>array(
			array('name'=>'title','title'=>'打印任务名称','type'=>'string'),
			array('name'=>'type','title'=>'打印机类型','type'=>'radio','options'=>'type','help'=>'打印机类型一定要选择正确，不然会影响大于效果（V9打印出来的是宽纸效果，其他两个是窄纸效果！）'),
			array('name'=>'printerid','title'=>'选择打印机','type'=>'select','show'=>array('type <='=>3),'options'=>'printer','help'=>'（<red>只显示非门店，非商户的打印机</red>）前往设置添加，打印机：<a href="javascript:;" ng-click="access_json(\'jy_print/print/\')">./index.php?plugin=jy_print&action=print</a>'),			
			array('name'=>'printerid','title'=>'选择打印机','type'=>'select','show'=>array('type'=>array(7,9)),'options'=>'printer7','help'=>'（<red>只显示非商户的打印机</red>）前往设置添加，打印机：<a href="javascript:;" ng-click="access_json(\'jy_feieyun/print/\')">./index.php?plugin=jy_feieyun&action=print</a>'),
			array('name'=>'printerid','title'=>'选择打印机','type'=>'select','show'=>array('type'=>array(8,10)),'options'=>'printer8','help'=>'（<red>只显示非商户的打印机</red>）前往设置添加，打印机：<a href="javascript:;" ng-click="access_json(\'jy_xpyun/print/\')">./index.php?plugin=jy_xpyun&action=print</a>'),	
			array('title'=>'【本地打印机】:如果同门店，同打印时机绑定了多个打印任务，<red>手动打印时：优先只打印【本地打印机】；自动打印时：只触发【非本地打印机】</red>','type'=>'tip','show'=>array('type'=>18)),
			array('name'=>'print_top','title'=>'打印头部','type'=>'text','show'=>array('type'=>array(0,1,2,3,4,5,6,7,8,9,10)),'help'=>'选择自定义模板后，本设置不生效'),
			array('name'=>'print_remark','title'=>'打印温馨提示','type'=>'text','show'=>array('type'=>array(0,1,2,3,4,5,6,7,8,9,10)),'help'=>'留空不打印','help'=>'选择自定义模板后，本设置不生效'),
			array('name'=>'print_nums','title'=>'打印联数','type'=>'string','show'=>array('type'=>array(0,1,2,3,4,5,6,7,8,9,10))),
			array('name'=>'print_status','title'=>'状态','type'=>'radio','options'=>'status'),
			)
	);
	$return['printer']=cfc('jy_print')->getlist(0,0,1);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['print_nums']>10){
		exi('节约环保！打印联数不能大于10。','error');
	}
	$data=array_fields(array('print_remark','print_ewm','print_top','print_type','print_status','title','type','printerid','print_nums'),$postdata);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_goodserpfw_printer',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['merchid']=$this->merchid;
		pdo_insert('jy_goodserpfw_printer',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$post=get_postdata();
	$update=array('print_status'=>$post['print_status']);
	if($update){
		pdo_update('jy_goodserpfw_printer',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_goodserpfw_printer',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$this->merchid));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_goodserpfw_printer',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$this->merchid));
			}
		}
	}
	exi('删除成功！');
}